package com.gzhryc.shared_device.oem.h5.agent;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.json.JsonTools;
import com.gzhryc.shared_device.bxscn.code.BXSCNCostConfig;
import com.gzhryc.shared_device.bxscn.code.dao.db.BXSCNFeeTemplate;
import com.gzhryc.shared_device.bxscn.code.dao.db.BXSCNShopFeeTemplate;
import com.gzhryc.shared_device.bxscn.code.services.BXSCNFeeTemplateService;
import com.gzhryc.shared_device.bxscn.code.services.BXSCNShopFeeTemplateService;
import com.gzhryc.shared_device.cdb.code.CDBCostConfig;
import com.gzhryc.shared_device.cdb.code.dao.db.CDBFeeTemplate;
import com.gzhryc.shared_device.cdb.code.services.CDBFeeTemplateService;
import com.gzhryc.shared_device.cdcz.code.CDCZCostConfig;
import com.gzhryc.shared_device.cdcz.code.dao.db.CDCZFeeTemplate;
import com.gzhryc.shared_device.cdcz.code.dao.models.CDCZPowerIntervalMoneyContent;
import com.gzhryc.shared_device.cdcz.code.services.CDCZFeeTemplateService;
import com.gzhryc.shared_device.common.device.dao.db.DeviceType;
import com.gzhryc.shared_device.common.device.services.DeviceTypeService;
import com.gzhryc.shared_device.oem.code.ConfigUtils;
import com.gzhryc.shared_device.oem.code.business.EBusinessType;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlaceBusinessConfig;
import com.gzhryc.shared_device.oem.h5.agent.dto.api.v1.cdb.CDBPlaceCostConfig;
import com.gzhryc.shared_device.oem.h5.agent.dto.api.v1.cdcz.PlaceCDCZCostConfig;
import com.gzhryc.shared_device.oem.h5.agent.dto.api.v1.bxscn.BXSCNPlaceCostConfig;
import com.gzhryc.shared_device.oem.h5.agent.dto.api.v1.pcl.PCLPlaceCostConfig;
import com.gzhryc.shared_device.oem.h5.agent.dto.api.v1.place.PlaceBusinessConfigListResponse;
import com.gzhryc.shared_device.pcl.PCLSetMealCostContent;
import com.gzhryc.shared_device.pcl.code.dao.db.PCLFeeTemplate;
import com.gzhryc.shared_device.pcl.code.services.PCLFeeTemplateService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DeviceCostConfigUtils {

    public static PlaceBusinessConfigListResponse.Item buildCostConfigItem(PlaceBusinessConfig businessConfig){
        if(EBusinessType.CDCZ.index().equals(businessConfig.getBusinessType())){
            return getPlaceCDCZCostConfig(businessConfig);
        }else if(EBusinessType.CDB.index().equals(businessConfig.getBusinessType())){
            return getPlaceCDBCostConfig(businessConfig);
        }else if(EBusinessType.BXSCN.index().equals(businessConfig.getBusinessType())){
            return getPlaceBXSCNCostConfig(businessConfig);
        }else if(EBusinessType.BXSCN_SHOP.index().equals(businessConfig.getBusinessType())){
            return getPlaceBXSCNShopCostConfig(businessConfig);
        }else if(EBusinessType.PCL.index().equals(businessConfig.getBusinessType())){
            return getPlacePCLCostConfig(businessConfig);
        }
        return null;
    }

    private static PlaceCDCZCostConfig getPlaceCDCZCostConfig(PlaceBusinessConfig businessConfig) {
        PlaceCDCZCostConfig costConfig = new PlaceCDCZCostConfig();
        costConfig.setBusinessType(businessConfig.getBusinessType());
        costConfig.setBusinessTypeName(businessConfig.getBusinessTypeName());
        costConfig.setFeeTemplateId(businessConfig.getFeeTemplateId());
        costConfig.setDeviceCount(businessConfig.getDeviceCount());
        CDCZFeeTemplate feeTemplate = CDCZFeeTemplateService.self().getById(businessConfig.getFeeTemplateId());
        if (feeTemplate != null) {
            costConfig.setId(feeTemplate.getId());
            costConfig.setTitle(feeTemplate.getTitle());
            costConfig.setMinWalletPrice(feeTemplate.getMinWalletMoneyStr());
            costConfig.setMaxChargeTimesMinute(feeTemplate.getMaxChargeTimesMinute());
            costConfig.setMaxChargeElectricity(feeTemplate.getMaxChargeElectricity());
            if (StringTools.isNotBlank(feeTemplate.getTimeSetMeal())) {
                costConfig.setTimeSetMeal(Arrays.asList(feeTemplate.getTimeSetMeal().split(",")));
            }
            if (StringTools.isNotBlank(feeTemplate.getPimContent())) {
                CDCZPowerIntervalMoneyContent content = JsonTools.fromJson(feeTemplate.getPimContent(), CDCZPowerIntervalMoneyContent.class);
                costConfig.setPimItems(new ArrayList<>());
                for (CDCZPowerIntervalMoneyContent.Item item : content.getItems()) {
                    PlaceCDCZCostConfig.PimItem pimItem = new PlaceCDCZCostConfig.PimItem();
                    pimItem.setMinPower(item.getMinPower());
                    pimItem.setMaxPower(item.getMaxPower());
                    pimItem.setPrice(item.getMoneyStr());
                    costConfig.getPimItems().add(pimItem);
                }
            }
        }else{
            CDCZCostConfig config = ConfigUtils.getCDCZCostConfig();
            costConfig.setTitle("全局模版");
            costConfig.setMinWalletPrice(config.getMinWalletMoneyStr());
            costConfig.setMaxChargeTimesMinute(config.getMaxTimesMinute());
            costConfig.setMaxChargeElectricity(config.getMaxChargeElectricity());
            if (StringTools.isNotBlank(config.getTimeSetMeal())) {
                costConfig.setTimeSetMeal(Arrays.asList(config.getTimeSetMeal().split(",")));
            }
            if (StringTools.isNotBlank(config.getPimContent())) {
                CDCZPowerIntervalMoneyContent content = JsonTools.fromJson(config.getPimContent(), CDCZPowerIntervalMoneyContent.class);
                costConfig.setPimItems(new ArrayList<>());
                for (CDCZPowerIntervalMoneyContent.Item item : content.getItems()) {
                    PlaceCDCZCostConfig.PimItem pimItem = new PlaceCDCZCostConfig.PimItem();
                    pimItem.setMinPower(item.getMinPower());
                    pimItem.setMaxPower(item.getMaxPower());
                    pimItem.setPrice(item.getMoneyStr());
                    costConfig.getPimItems().add(pimItem);
                }
            }
        }
        return costConfig;
    }

    private static CDBPlaceCostConfig getPlaceCDBCostConfig(PlaceBusinessConfig businessConfig){
        CDBPlaceCostConfig costConfig = new CDBPlaceCostConfig();
        costConfig.setBusinessType(businessConfig.getBusinessType());
        costConfig.setBusinessTypeName(businessConfig.getBusinessTypeName());
        costConfig.setFeeTemplateId(businessConfig.getFeeTemplateId());
        costConfig.setDeviceCount(businessConfig.getDeviceCount());
        CDBFeeTemplate feeTemplate = CDBFeeTemplateService.self().getById(businessConfig.getFeeTemplateId());
        if(feeTemplate != null){
            costConfig.setId(feeTemplate.getId());
            costConfig.setTitle(feeTemplate.getTitle());
            costConfig.setDepositPrice(feeTemplate.getDepositMoneyStr());
            costConfig.setDayMaxPrice(feeTemplate.getDayMaxMoneyStr());
            costConfig.setPayIntervalPrice(feeTemplate.getPayIntervalMoneyStr());
            costConfig.setFreeTimes(feeTemplate.getFreeTimes());
        }else{
            CDBCostConfig config = ConfigUtils.getCDBCostConfig();
            costConfig.setTitle("全局模版");
            costConfig.setDepositPrice(config.getDepositMoneyStr());
            costConfig.setDayMaxPrice(config.getDayMaxMoneyStr());
            costConfig.setPayIntervalPrice(config.getPayIntervalMoneyStr());
            costConfig.setFreeTimes(config.getFreeTimes());
        }
        return costConfig;
    }

    private static BXSCNPlaceCostConfig getPlaceBXSCNCostConfig(PlaceBusinessConfig businessConfig){
        BXSCNPlaceCostConfig costConfig = new BXSCNPlaceCostConfig();
        costConfig.setBusinessType(businessConfig.getBusinessType());
        costConfig.setBusinessTypeName(businessConfig.getBusinessTypeName());
        costConfig.setFeeTemplateId(businessConfig.getFeeTemplateId());
        costConfig.setDeviceCount(businessConfig.getDeviceCount());
        BXSCNFeeTemplate feeTemplate = BXSCNFeeTemplateService.self().getById(businessConfig.getFeeTemplateId());
        if(feeTemplate != null){
            costConfig.setId(feeTemplate.getId());
            costConfig.setTitle(feeTemplate.getTitle());
            costConfig.setDepositPrice(feeTemplate.getDepositMoneyStr());
            costConfig.setDayMaxPrice(feeTemplate.getDayMaxMoneyStr());
            costConfig.setPayIntervalPrice(feeTemplate.getPayIntervalMoneyStr());
            costConfig.setFreeTimes(feeTemplate.getFreeTimes());
        }else{
            BXSCNCostConfig config = ConfigUtils.getBXSCNCostConfig();
            costConfig.setTitle("全局模版");
            costConfig.setDepositPrice(config.getDepositMoneyStr());
            costConfig.setDayMaxPrice(config.getDayMaxMoneyStr());
            costConfig.setPayIntervalPrice(config.getPayIntervalMoneyStr());
            costConfig.setFreeTimes(config.getFreeTimes());
        }
        return costConfig;
    }
    
    private static PlaceBusinessConfigListResponse.Item getPlaceBXSCNShopCostConfig(PlaceBusinessConfig businessConfig){
    	PlaceBusinessConfigListResponse.Item costConfig = new PlaceBusinessConfigListResponse.Item();
        costConfig.setBusinessType(businessConfig.getBusinessType());
        costConfig.setBusinessTypeName(businessConfig.getBusinessTypeName());
        costConfig.setDeviceCount(businessConfig.getDeviceCount());
        BXSCNShopFeeTemplate feeTemplate = BXSCNShopFeeTemplateService.self().getById(businessConfig.getFeeTemplateId());
        if(feeTemplate != null){
        	costConfig.setFeeTemplateId(businessConfig.getFeeTemplateId());
            costConfig.setTitle(feeTemplate.getTitle());
        }else{
        	costConfig.setTitle("根据电池类型不同收费（全局配置）");
            
        }
        return costConfig;
    }

    private static PCLPlaceCostConfig getPlacePCLCostConfig(PlaceBusinessConfig businessConfig){
        PCLPlaceCostConfig costConfig = new PCLPlaceCostConfig();
        costConfig.setBusinessType(businessConfig.getBusinessType());
        costConfig.setBusinessTypeName(businessConfig.getBusinessTypeName());
        costConfig.setDeviceCount(businessConfig.getDeviceCount());
        if(StringTools.isNotBlank(businessConfig.getCostContent())){
            PCLSetMealCostContent costContent = JsonTools.fromJson(businessConfig.getCostContent(), PCLSetMealCostContent.class);
            if (costContent != null && costContent.getItems() != null && costContent.getItems().size() > 0) {
                costConfig.setItems(new ArrayList<>());
                List<String> deviceTypeIds = new ArrayList<>();
                List<Long> feeTemplateIds = new ArrayList<>();
                for (PCLSetMealCostContent.Item item : costContent.getItems()) {
                    deviceTypeIds.add(item.getDeviceTypeId());
                    feeTemplateIds.add(item.getFeeTemplateId());
                }

                List<DeviceType> deviceTypeList = DeviceTypeService.self().findByIds(deviceTypeIds);
                List<PCLFeeTemplate> feeTemplateList = PCLFeeTemplateService.self().findJsonByIds(feeTemplateIds);

                for (PCLSetMealCostContent.Item item : costContent.getItems()) {
                    PCLPlaceCostConfig.Item temp = new PCLPlaceCostConfig.Item();
                    temp.setDeviceTypeId(item.getDeviceTypeId());
                    temp.setFeeTemplateId(item.getFeeTemplateId());

                    if(deviceTypeList != null && deviceTypeList.size() > 0) {
                        for(DeviceType deviceType : deviceTypeList) {
                            if(deviceType.getId().equals(temp.getDeviceTypeId())) {
                                temp.setDeviceTypeName(deviceType.getName());
                                break;
                            }
                        }
                    }
                    if(feeTemplateList != null && feeTemplateList.size() > 0) {
                        for (PCLFeeTemplate feeTemplate : feeTemplateList) {
                            if(feeTemplate.getId().equals(temp.getFeeTemplateId())) {
                                temp.setFeeTemplateName(feeTemplate.getTitle());
                                break;
                            }
                        }
                    }
                    costConfig.getItems().add(temp);
                }
            }
        }
        return costConfig;
    }
}
